﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using yncTech.Common;
using EntitySpaces.Interfaces;

public partial class Admin_FinMov_FinMov001e : AdminBasePage
{
    decimal shipID = -1;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[FinShippingMetadata.ColumnNames.ShipID] != null)
        {
            shipID = decimal.Parse(Request.QueryString[FinShippingMetadata.ColumnNames.ShipID]);
        }

        if (!IsPostBack)
        {
            FinShippingBoxesCollection fsp = new FinShippingBoxesCollection();
            fsp.Query.Where(fsp.Query.ShipID == shipID);
            fsp.Query.Load();

            var pkgNotDone = fsp.Where(r => string.IsNullOrEmpty(r.PackageNo)).Count() > 0;

            if (pkgNotDone)
            {
                btnSave.Enabled = false;
                lbMessage.Text = "尚未完成打包,无法提交审核!";
                
            }
          
            GridDataBind();
        }


    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        lbMessage.Text = "";
        try
        {
            if (chkAudit1.SelectedValue.Length == 0)
            {
                lbMessage.Text = "请选择复核人!";
                return;
            }          
           
            FinShipping fsp = new FinShipping();
            fsp.LoadByPrimaryKey(shipID);
            fsp.Check2 = chkAudit1.SelectedValue;
            fsp.UpdatedDttm = DateTime.Now;
            fsp.UpdatedBy = LoginUserInfo.CurrentUser.UserName;
            fsp.ShippingStatus = ConstantValue.ShippingStatusCode.PackageCheck;
            fsp.Save();
          

            WebHelper.CloseDialog(true);
        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }
    }

    //TO DO
    void TriggerMail()
    {
        //Send mail to CF/ACC Manager

        //Send mail to all user(same company)
    }

    void GridDataBind()
    {

       

        TSUserInfoQuery tq1 = new TSUserInfoQuery("tq1");
        TSUserRolesQuery tq2 = new TSUserRolesQuery("tq2");
        tq1.InnerJoin(tq2).On(tq1.UserID == tq2.UserID);
        tq1.Where(tq1.UserID != LoginUserInfo.CurrentUser.UserID);
        tq1.Select(tq1.UserName.As("Value"), (tq1.UserName + " - " + tq1.Email).As("Text"));
        var dataTable = tq1.LoadDataTable();
        chkAudit1.DataSource = dataTable;
        chkAudit1.DataTextField = "Text";
        chkAudit1.DataValueField = "Value";
        chkAudit1.DataBind();



    }


    private string getDescByCode(string code)
    {
        TSCodeTableCollection tSCodeTableCollection = new TSCodeTableCollection();
        tSCodeTableCollection.Query.Where(tSCodeTableCollection.Query.Code == code);
        tSCodeTableCollection.Query.Load();
        if (tSCodeTableCollection.Count == 1)
        {
            return tSCodeTableCollection[0].CodeDesc;
        }
        else
        {
            return "";
        }
    }

    private string getThirdName(string Id)
    {
        BFThirdUnit bFThirdUnit = new BFThirdUnit();
        bFThirdUnit.LoadByPrimaryKey(decimal.Parse(Id));
        if (bFThirdUnit.es.HasData)
        {
            return bFThirdUnit.Name;
        }
        else
        {
            return "";
        }
    }
}